package com.tjetc;

import java.util.Scanner;

public class _12Homework1 {
    /*
        (1)在main方法中启动两个线程
        (2)第1个线程循环随机打印100以内的整数
        (3)直到第2个线程从键盘读取了“Q”命令。
     */
    public static void main(String[] args) {
        A a = new A();
        B b = new B(a);
        a.start();
        b.start();
    }


}


class A extends Thread {
    private boolean loop = true;


    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    @Override
    public void run() {
        // 1-100数字
        while (loop) {
            System.out.println((int) (Math.random() * 100 + 1));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class B extends Thread {
    private A a;
    private Scanner scanner = new Scanner(System.in);
    public B(A a) {
        this.a = a;
    }

    @Override
    public void run() {
        // 主要作用就是当输入Q的时候 改变loop变量
        while (true) {
            // 要求是大写
            System.out.println("请输入大写Q结束");
            char key = scanner.next().toUpperCase().charAt(0);
            if (key == 'Q') {
                a.setLoop(false);
                break;
            }
        }
    }
}